package com.loubobooo.personal.blog.controller.portal;

import com.loubobooo.personal.blog.common.Const;
import com.loubobooo.personal.blog.entity.vo.*;
import com.loubobooo.personal.blog.service.ArticleService;
import com.loubobooo.personal.blog.util.JsonUtil;
import com.loubobooo.personal.blog.util.RedisPoolUtil;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

/**
 * 文章的controller
 */
@Controller public class ArticleController {

    @Autowired private ArticleService articleService;

    //文章详情页显示
    @RequestMapping(value = "/article/{articleId}")
    //适合RESTful
    public ModelAndView ArticleDetailView(@PathVariable("articleId") Integer articleId){
        ModelAndView modelAndView = new ModelAndView();

        //文章信息，分类，标签，作者，评论
        ArticleDetailVo articleDetailVo = null;
        String cacheKey = Const.REDIS_KEY.ARTICLE_DETAIL_SHOW + "_" + articleId;

        String cacheValue = RedisPoolUtil.get(cacheKey);
        if(StringUtils.isBlank(cacheValue)){
            articleDetailVo = articleService.getArticleDetailById(articleId);
            // 缓存到redis
            RedisPoolUtil.set(cacheKey, JsonUtil.obj2String(articleDetailVo));
        }else{
            articleDetailVo = JsonUtil.string2Obj(cacheValue, new TypeReference<ArticleDetailVo>(){});
        }
        if (articleDetailVo != null) {

            modelAndView.addObject("articleDetailVo", articleDetailVo);
            //相关文章
            Integer parentCategoryId = articleService.getArticleById(1, articleId).getArticleParentCategoryId();
            Integer childCategoryId = articleService.getArticleById(1, articleId).getArticleChildCategoryId();
            List<ArticleVO> similarArticleList = articleService
                    .listArticleWithSameCategory(1, parentCategoryId, childCategoryId, 5);
            modelAndView.addObject("similarArticleList", similarArticleList);

            //猜你喜欢
            List<ArticleVO> mostViewArticleList = articleService.listArticleByViewCount(1, 5);
            modelAndView.addObject("mostViewArticleList", mostViewArticleList);
            //获取下一篇文章
            ArticleVO afterArticle = articleService.getAfterArticle(1, articleId);
            modelAndView.addObject("afterArticle", afterArticle);
            //获取上一篇文章
            ArticleVO preArticle = articleService.getPreArticle(1, articleId);
            modelAndView.addObject("preArticle", preArticle);
            modelAndView.setViewName("Home/Page/articleDetail");
        }
        else {
            modelAndView.setViewName("Home/Error/404");
        }
        return modelAndView;//不会被解析为跳转路径，而是直接写入HTTP response body中
    }

    //TODO:文章点赞数增加
    @RequestMapping(value = "/article/addLike/{id}", method = {
            RequestMethod.POST })
    @ResponseBody
    public Integer increaseLikeCount(@PathVariable("id") Integer id){
        ArticleVO articleVO = articleService.getArticleById(1, id);
        int articleCount = articleVO.getArticleLikeCount();
        articleVO.setArticleLikeCount(articleCount + 1);
        articleService.updateArticle(id, articleVO);
        return articleCount + 1;
    }

    //文章访问量数增加
    @RequestMapping(value = "/article/addView/{id}", method = {
            RequestMethod.POST }) @ResponseBody public Integer increaseViewCount(@PathVariable("id") Integer id){
        ArticleVO articleVO = articleService.getArticleById(1, id);
        int articleCount = articleVO.getArticleViewCount();
        articleVO.setArticleViewCount(articleCount + 1);
        articleService.updateArticle(id, articleVO);
        return articleCount + 1;
    }

    //文章信息修改提交
    @RequestMapping(value = "/editArticleSubmit", method = RequestMethod.POST) public String editArticleSubmit(
            Integer id, ArticleVO articleVO){

        articleService.updateArticle(id, articleVO);
        return "redirect:articleList.action";
    }

    //文章搜索实现
    @RequestMapping("/search")
    public ModelAndView SearchPageView(HttpServletRequest request, Model model) {
        ModelAndView modelAndView = new ModelAndView();
        //设置每页显示的数量
        int pageSize = 10;
        String query = request.getParameter("query");
        List<ArticleSearchVo> articleSearchVoList = articleService
                .listSearchResultByPage(1, request, model, null, pageSize, query);
        if (articleSearchVoList != null) {
            modelAndView.addObject("articleSearchVoList", articleSearchVoList);
        }
        else {
            modelAndView.addObject("articleSearchVoList", null);
        }
        modelAndView.setViewName("Home/Page/search");
        return modelAndView;
    }

    //文章搜索分页实现
    @RequestMapping("/p/{pageNo}/search")
    public ModelAndView SearchPageByPageView(
            HttpServletRequest request, Model model, @PathVariable("pageNo") Integer pageNo){
        ModelAndView modelAndView = new ModelAndView();
        //设置每页显示的数量
        int pageSize = 10;
        String query = request.getParameter("query");
        List<ArticleSearchVo> articleSearchVoList = articleService
                .listSearchResultByPage(1, request, model, pageNo, pageSize, query);
        modelAndView.addObject("articleSearchVoList", articleSearchVoList);
        modelAndView.setViewName("Home/Page/search");
        return modelAndView;
    }

}
